package com.be.beadmin.system.impl.ProductMaterialServiceImpl;

import com.be.beadmin.design.dto.output.ProductBomOverviewEx;
import com.be.beadmin.design.entity.ProductBomOverview;
import com.be.beadmin.material.entity.PickingOverView;
import com.be.beadmin.material.entity.PickingReccieveDetails;
import com.be.beadmin.material.mapper.PickingReccieveDetailsMapper;
import com.be.beadmin.material.mapper.PickingReccieveDetailsMapperEx;
import com.be.beadmin.material.repository.PickingOverViewRepository;
import com.be.beadmin.material.repository.PickingReccieveDetailsRepository;
import com.be.beadmin.product.entity.WorkOrderInfor;
import com.be.beadmin.product.service.WorkOrderInforService;
import com.be.beadmin.system.service.ProductMaterialService.PickingReccieveDetailsPmService;
import com.be.beadmin.system.service.ProductMaterialService.WorkOrderInforPmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @author: Jiaming Zheng
 * @Date: Create in 17:46 2023/6/24
 * @Modified By:
 **/
@Service
public class PickingReccieveDetailsPmServiceImpl implements PickingReccieveDetailsPmService {

    @Autowired
    private PickingReccieveDetailsRepository pickingReccieveDetailsRepository;
    @Autowired
    private PickingOverViewRepository pickingOverViewRepository;


    @Autowired
    private WorkOrderInforPmService workOrderInforPmService;
    /**
     * 收料记录
     *
     * @param workOrderInfor
     */
    @Override
    public void reccieveDetails(WorkOrderInfor workOrderInfor) {
        PickingOverView pickingOverView = pickingOverViewRepository.findPickingOverViewByProductCodeAndDelete(workOrderInfor.getBatchProductCode(), 0);
        List<PickingReccieveDetails> reccieveDetails = new ArrayList<>();
        for (ProductBomOverviewEx productBomOverview : workOrderInforPmService.getBomOverview(workOrderInfor.getId())) {
            PickingReccieveDetails reccieveDetail1 = new PickingReccieveDetails();
            reccieveDetail1.setProductCode(workOrderInfor.getBatchProductCode());
            reccieveDetail1.setPickingNo(pickingOverView.getPickingNo());
            reccieveDetail1.setMaterialCode(productBomOverview.getMaterialCode());
            reccieveDetail1.setMaterialNeedQty(productBomOverview.getMaterialQty());

            reccieveDetails.add(reccieveDetail1);
        }
        pickingReccieveDetailsRepository.saveAll(reccieveDetails);


    }
}
